package com.whmskj.xjlhbc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.DailyProductionPerformanceReport;
import com.whmskj.xjlhbc.domain.Jx05dStaffIncentiveAndSanctionLo;
import com.whmskj.xjlhbc.domain.PersonnelManagementDataSheets;
import com.whmskj.xjlhbc.domain.ThePlanesStrappedToATable;
import com.whmskj.xjlhbc.dto.JxQueryDto;
import com.whmskj.xjlhbc.mapper.DailyProductionPerformanceReportMapper;
import com.whmskj.xjlhbc.mapper.ThePlanesStrappedToATableMapper;
import com.whmskj.xjlhbc.service.IDailyProductionPerformanceReportService;
import com.whmskj.xjlhbc.service.IJx05dStaffIncentiveAndSanctionLoService;
import com.whmskj.xjlhbc.service.IPersonnelManagementDataSheetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DailyProductionPerformanceReportServiceImpl
        extends ServiceImpl<DailyProductionPerformanceReportMapper, DailyProductionPerformanceReport> implements IDailyProductionPerformanceReportService {

    @Autowired
    private IPersonnelManagementDataSheetsService personnelManagementDataSheetsService;

    @Autowired
    private ThePlanesStrappedToATableMapper thePlanesStrappedToATableMapper;

    @Autowired
    private IJx05dStaffIncentiveAndSanctionLoService jx05dStaffIncentiveAndSanctionLoService;


    @Override
    public Object jxTj(JxQueryDto param) {
        String month = param.getMonth();
        List<String> date = getDate(month);
        String start = date.get(0);
        String end = date.get(1);
        List<DailyProductionPerformanceReport> list = lambdaQuery().between(DailyProductionPerformanceReport::getStatisticsDate, start,end)
                .eq(StringUtils.hasText(param.getClasses()), DailyProductionPerformanceReport::getClassesInTheClassroom, param.getClasses())
                .like(StringUtils.hasText(param.getPosition()), DailyProductionPerformanceReport::getDutyStations, param.getPosition())
                .and(StringUtils.hasText(param.getNameWorkNumber()), queryWrapper ->
                        queryWrapper.like(DailyProductionPerformanceReport::getName, param.getNameWorkNumber()).or()
                                .like(DailyProductionPerformanceReport::getWorkNumber, param.getNameWorkNumber()))
                .list();
        Map<String, Object> map = new HashMap<>();
        map.put("cq", list.size());
        map.put("jx", String.format("%.2f",list.stream().mapToDouble(DailyProductionPerformanceReport::getProductionWages).sum()));
        map.put("jc", String.format("%.2f",list.stream().mapToDouble(DailyProductionPerformanceReport::getAmountOfIncentivesAndSanctions).sum()));
        return map;
    }

    @Override
    public List<DailyProductionPerformanceReport> queryList(JxQueryDto param) {
        Page.startPage();
        String month = param.getMonth();
        List<String> date = getDate(month);
        String start = date.get(0);
        String end = date.get(1);
        List<String> className = personnelManagementDataSheetsService.getClassName();
        LambdaQueryWrapper<PersonnelManagementDataSheets> personnelManagementDataSheetsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        personnelManagementDataSheetsLambdaQueryWrapper.in(PersonnelManagementDataSheets::getClass22, className);
        List<PersonnelManagementDataSheets> personnelManagementDataSheetsList = personnelManagementDataSheetsService.list(personnelManagementDataSheetsLambdaQueryWrapper);

        List<String> names = personnelManagementDataSheetsList.stream().map(PersonnelManagementDataSheets::getFe1e).distinct().collect(Collectors.toList());
        List<DailyProductionPerformanceReport> list = lambdaQuery().between(DailyProductionPerformanceReport::getStatisticsDate, start,end)
                .eq(StringUtils.hasText(param.getClasses()), DailyProductionPerformanceReport::getClassesInTheClassroom, param.getClasses())
                .like(StringUtils.hasText(param.getPosition()), DailyProductionPerformanceReport::getDutyStations, param.getPosition())
                .and(StringUtils.hasText(param.getNameWorkNumber()), queryWrapper ->
                        queryWrapper.like(DailyProductionPerformanceReport::getName, param.getNameWorkNumber()).or()
                                .like(DailyProductionPerformanceReport::getWorkNumber, param.getNameWorkNumber()))
                .in(DailyProductionPerformanceReport::getName,names)
                .list();
        if(names.isEmpty()) return new ArrayList<>();
        return personnelManagementDataSheetsList.stream().map(personnel -> {
            List<DailyProductionPerformanceReport> collect = list.stream().filter(f -> f.getName().equals(personnel.getFe1e())).collect(Collectors.toList());
            DailyProductionPerformanceReport report = new DailyProductionPerformanceReport();
            report.setName(personnel.getFe1e());
            report.setDutyStations(personnel.getPositions1());
            report.setWorkNumber(personnel.getGender2()+"/"+personnel.getCode());
            report.setProductionWages(collect.stream().mapToDouble(DailyProductionPerformanceReport::getProductionWages).sum());
            report.setAmountOfIncentivesAndSanctions(collect.stream().mapToDouble(DailyProductionPerformanceReport::getAmountOfIncentivesAndSanctions).sum());
            report.setCumulativeProduction(collect.stream().mapToDouble(DailyProductionPerformanceReport::getCumulativeProduction).sum());
            double hours = collect.stream().mapToDouble(DailyProductionPerformanceReport::getHoursOfAttendance).sum();
            report.setHoursOfAttendance(hours/12);
            return report;
        }).collect(Collectors.toList());

    }

    @Override
    public List<DailyProductionPerformanceReport> queryUserDetailList(JxQueryDto param) {
        Page.startPage();
        String month = param.getMonth();
        List<String> date = getDate(month);
        String start = date.get(0);
        String end = date.get(1);
        String nameWorkNumber = param.getNameWorkNumber();
        List<DailyProductionPerformanceReport> list = lambdaQuery().between(DailyProductionPerformanceReport::getStatisticsDate, start,end)
                .eq(StringUtils.hasText(nameWorkNumber), DailyProductionPerformanceReport::getName, nameWorkNumber)
                .list();

        List<String> names = list.stream().map(DailyProductionPerformanceReport::getName).distinct().collect(Collectors.toList());
        if(names.isEmpty()) return new ArrayList<>();
        LambdaQueryWrapper<ThePlanesStrappedToATable> thePlanesStrappedToATableLambdaQueryWrapper = new LambdaQueryWrapper<>();
        thePlanesStrappedToATableLambdaQueryWrapper.in(ThePlanesStrappedToATable::getNameOfPersonnel, names)
                .between(ThePlanesStrappedToATable::getF7ba, start,end);
        List<ThePlanesStrappedToATable> thePlanesStrappedToATables = thePlanesStrappedToATableMapper.selectList(thePlanesStrappedToATableLambdaQueryWrapper);

        LambdaQueryWrapper<Jx05dStaffIncentiveAndSanctionLo> jx05dStaffIncentiveAndSanctionLoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        jx05dStaffIncentiveAndSanctionLoLambdaQueryWrapper.in(Jx05dStaffIncentiveAndSanctionLo::getName, names)
                .between(Jx05dStaffIncentiveAndSanctionLo::getDateOfRewardAndSanction, start,end);
        List<Jx05dStaffIncentiveAndSanctionLo> jx05dStaffIncentiveAndSanctionLos = jx05dStaffIncentiveAndSanctionLoService.list(jx05dStaffIncentiveAndSanctionLoLambdaQueryWrapper);
        list.forEach(item->{
            ThePlanesStrappedToATable thePlanesStrappedToATable = thePlanesStrappedToATables.stream().filter(f -> f.getNameOfPersonnel().equals(item.getName()) && f.getF7ba().equals(item.getStatisticsDate())).findFirst().orElseGet(ThePlanesStrappedToATable::new);
            List<Jx05dStaffIncentiveAndSanctionLo> jx05dStaffIncentiveAndSanctionLosList = jx05dStaffIncentiveAndSanctionLos.stream().filter(f -> f.getName().equals(item.getName()) && f.getDateOfRewardAndSanction().equals(item.getStatisticsDate())).collect(Collectors.toList());
            String f657 = thePlanesStrappedToATable.getF657();
            if(StringUtils.hasText(f657)){
                item.getJthList().addAll(Arrays.stream(thePlanesStrappedToATable.getF657().split(",")).collect(Collectors.toList()));
            }
            item.setJcList(jx05dStaffIncentiveAndSanctionLosList);
        });
        return list;
    }

    private List<String> getDate(String month){
        LocalDate ld;
        LocalDate ld1;
        if(StringUtils.hasText(month)){
            ld = LocalDate.parse(month + "-20");
            ld1 = ld.minusMonths(1).withDayOfMonth(21);
        }else{
            int dayOfMonth = LocalDate.now().getDayOfMonth();
            if(dayOfMonth > 20){
                ld1 = LocalDate.now().withDayOfMonth(21);
                ld = ld1.plusMonths(1).withDayOfMonth(20);
            }else{
                ld1 = LocalDate.now().minusMonths(1).withDayOfMonth(21);
                ld = LocalDate.now().withDayOfMonth(20);
            }
        }
        List<String> list = new ArrayList<>();
        list.add(ld1.toString());
        list.add(ld.toString());
        return list;
    }
}
